/*
     Copyright 2007 Joseph M. Ferner

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using OpenServers.Net.FtpServer.State;

namespace OpenServers.Net.FtpServer.Command {
    /// <summary>
    ///     <para>
    ///         <b>PASSWORD (PASS)</b><br/>
    ///         The argument field is a Telnet string specifying the user's
    ///         password.  This command must be immediately preceded by the
    ///         user name command, and, for some sites, completes the user's
    ///         identification for access control.  Since password
    ///         information is quite sensitive, it is desirable in general
    ///         to "mask" it or suppress typeout.  It appears that the
    ///         server has no foolproof way to achieve this.  It is
    ///         therefore the responsibility of the user-FTP process to hide
    ///         the sensitive password information.
    ///     </para>
    ///     <para>
    ///         <b>Format</b><br/>
    ///         <code>PASS &lt;SP&gt; &lt;password&gt; &lt;CRLF&gt;</code>
    ///     </para>
    ///     <para>
    ///         <b>Success:</b>
    ///         <list type="bullet">
    ///             <item><term>230</term><description>User logged in, proceed.</description></item>
    ///             <item><term>332</term><description>Need account for login.</description></item>
    ///         </list>
    ///     </para>
    ///     <para>
    ///         <b>Errors:</b>
    ///         <list type="bullet">
    ///             <item><term>202</term><description>Command not implemented, superfluous at this site.</description></item>
    ///             <item><term>421</term><description>Service not available, closing control connection.</description></item>
    ///             <item><term>500</term><description>Syntax error, command unrecognized.</description></item>
    ///             <item><term>501</term><description>Syntax error in parameters or arguments.</description></item>
    ///             <item><term>503</term><description>Bad sequence of commands.</description></item>
    ///             <item><term>530</term><description>Not logged in.</description></item>
    ///         </list>
    ///     </para>
    /// 
    /// </summary>
    public class PasswordCommand : BaseCommand {

        /// <summary>
        /// Overrides the base functionality because a user doesn't have to be logged in.
        /// </summary>
        public override void Invoke(IFtpServer server, FtpState state, string arguments) {
            PerformCommand(server, state, arguments);
        }

        /// <summary>
        /// Validates the user.
        /// </summary>
        public override void PerformCommand(IFtpServer server, FtpState state, string arguments) {
            if (string.IsNullOrEmpty(arguments)) {
                state.Listener.Send(state, server.Messages.Get501());
            } else {
                state.User = server.SecurityProvider.ValidateUser(state.UserName, arguments);
                if (state.User != null) {
                    state.Listener.Send(state, server.Messages.Get230());
                } else {
                    state.Listener.Send(state, server.Messages.Get530());
                }
            }
        }
    }
}
